package NetAPI;

import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class ClientTest {
    public static void main(String[] args) {
        while (true) {
            try {
                Thread.sleep(300);
                Client();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void Client() {
        System.out.println("Client");
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress ip = InetAddress.getLocalHost();
            // 创建 Socket 对象，指明服务器端的 ip 和端口号
            socket = new Socket(ip, 8899);
            // 获取输出流，用于输出数据
            os = socket.getOutputStream();
            // 输出数据
            os.write("Hello, this is a message from client.".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
